//
//  KSViewController.swift
//  Bee
//
//  Created by kyson on 15/12/30.
//  Copyright © 2015年 kyson. All rights reserved.
//

import UIKit

class KSViewController: UIViewController,TaskPoolDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    required override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    internal func viewModelDidBinded(){}
    
    func doServiceWithName(serviceName : String){
        let serviceMediator = NetworkServiceMediator.init(nServiceName: serviceName)
        TaskPool.sharedPool.addDelegate(self)
        TaskPool.sharedPool.doTask(service: serviceMediator)
    }
    
    internal func taskPoolServiceDidFinished(service: Service,response: NetworkResponse){
        serviceDidFinished(service.serviceName,response: response)
    }

    func serviceDidFinished(serviceName: String,response: NetworkResponse){}
    

    override func viewWillDisappear(animated: Bool) {
        TaskPool.sharedPool.addDelegate(nil)
    }
}






extension KSViewController {
    
    private struct AssociatedKeys{
        static var pageId       = "CustomKey_pageId"
        static var viewModel    = "CustomKey_viewModel"
    }
    
    var pageId:String?{
        get{
            return objc_getAssociatedObject(self, &AssociatedKeys.pageId) as? String
        }
        set{
            if let newValue = newValue{
                objc_setAssociatedObject(self, &AssociatedKeys.pageId, newValue as String, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }
    
    var viewModel:BaseViewModel?{
        get{
            return objc_getAssociatedObject(self, &AssociatedKeys.viewModel) as? BaseViewModel
        }
        set{
            if let newValue = newValue{
                objc_setAssociatedObject(self, &AssociatedKeys.viewModel, newValue as BaseViewModel, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }
}






